﻿using System;
using System.Text;
using System.Web.Mvc;
using Lyh.Common.Parser;

namespace Lyh.Core.Result
{
    public class CustomJsonResult : ActionResult
    {
        public Encoding ContentEncoding { get; set; }
        public string ContentType { get; set; }
        public object Data { get; set; }
        public bool CamelCase { get; set; }
        public bool Format { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            var response = context.HttpContext.Response;
            response.ContentType = string.IsNullOrWhiteSpace(ContentType) ? "application/json" : ContentType;
            response.ContentEncoding = ContentEncoding ?? Encoding.UTF8;

            if (Data == null) return;
            var json = Data.ToJson(Format, CamelCase);
            response.Write(json);
        }
    }
}
